# Maintainer: Daniel M. Capella <polyzen@archlinux.org>

pkgname=typescript-language-server
pkgver=4.1.2
pkgrel=1
pkgdesc='Language Server Protocol (LSP) implementation for TypeScript using tsserver'
url=https://github.com/theia-ide/typescript-language-server
arch=('any')
license=('Apache')
depends=('typescript')
makedepends=('git' 'jq' 'yarn')
checkdepends=('npm')
source=("git+$url.git#tag=v$pkgver")
b2sums=('SKIP')

prepare() {
  cd $pkgname
  yarn --frozen-lockfile
}

build() {
  cd $pkgname
  yarn compile # Needs bin links
}

check() {
  cd $pkgname
  yarn test
}

package() {
  local mod_dir=/usr/lib/node_modules/$pkgname
  install -d "$pkgdir"/{usr/bin,$mod_dir}
  ln -s $mod_dir/lib/cli.mjs "$pkgdir"/usr/bin/$pkgname

  cd $pkgname
  # Emulate `npm prune --production`
  cp package.json{,.bak}
  read -ra devDependencies < <(jq -r '.devDependencies | keys | join(" ")' package.json)
  yarn remove --frozen-lockfile "${devDependencies[@]}"
  mv package.json{.bak,}

  cp -r lib package.json "$pkgdir"/$mod_dir
  chmod +x "$pkgdir"/$mod_dir/lib/cli.mjs
  install -Dm644 -t "$pkgdir"/usr/share/doc/$pkgname {CHANGELOG,README}.md
}
